//
//  TFLine1Vn.swift
//  TalkfunSDKDemo
//
//  Created by 莫瑞权 on 2022/5/8.
//  Copyright © 2022 Talkfun. All rights reserved.
//

import UIKit
public typealias TFLine1VnBlock = (_ msg:NSMutableDictionary) -> Void
public class TFLine1Vn: UIView {
    @objc public let array = NSMutableArray()
    @objc public let btnArray = NSMutableArray()
    @objc public var networkOperators:TFLine1VnBlock?
    
    @objc public func addRouteList(_ array:NSArray)
    {
        if array.count != self.array.count  {
            self.array.removeAllObjects()
            self.array.addObjects(from: array as! [Any])
            self.btnArray.removeAllObjects()
                
            
            if self.subviews.count>0{
               self.subviews.forEach({$0.removeFromSuperview()})
            }
              
            let top:CGFloat = 30
            for i in 0..<array.count {
                let line = "线路" + String.init(format: "%i", i+1)
              
                        
                        let button = UIButton()
                        button.tag = i
                button.setTitle(line, for: .normal)
                
                button.titleLabel!.font = UIFont.PingFang(size: 16)
                let buttonH:CGFloat = 22.5
                        //间距18.5
                        button.frame = CGRect(x: 0, y: top+(CGFloat(i)*buttonH)+(CGFloat(i)*18.5), width: self.frame.size.width, height: buttonH)
               
                        if (i==0) {
                            button.setTitleColor(UIColor(red: 23/255.0, green: 145/255.0, blue: 251/255.0, alpha: 1), for: .normal)
                             
                        }else
                        {
                            button.setTitleColor(UIColor(red: 255.0/255.0, green: 255.0/255.0, blue: 255.0/255.0, alpha: 1), for: .normal)
                            
                        }
                self.addSubview(button)
                self.btnArray.add(button)
                button.addTarget(self, action: #selector(self.buttonClick(_:)), for: .touchUpInside)
                       
                }
        }
       

    }
    @objc public func buttonClick(_ btn:UIButton)
    {
        

        if (btn.tag<self.array.count) {
            
          let dict = NSMutableDictionary()
            dict.setObject(String.init(format: "%i", btn.tag+1), forKey:"selectedSegmentIndex" as NSString)
           
            if (self.networkOperators != nil) {
                for i in 0..<self.btnArray.count {
                    let button:UIButton = self.btnArray[i] as! UIButton
              
                    button.setTitleColor(UIColor(red: 255.0/255.0, green: 255.0/255.0, blue: 255.0/255.0, alpha: 1), for: .normal)
                }
               
                btn.setTitleColor(UIColor(red: 23/255.0, green: 145/255.0, blue: 251/255.0, alpha: 1), for: .normal)
    
                                 
                    self.networkOperators!(dict)

               
            }
        }
        
    }

}
